package com.qyhl.module_practice.activity.detail;


import com.google.gson.Gson;
import com.qyhl.module_practice.common.PracticeUrl;
import com.qyhl.webtv.basiclib.utils.network.EasyHttp;
import com.qyhl.webtv.basiclib.utils.network.callback.SimpleCallBack;
import com.qyhl.webtv.basiclib.utils.network.exception.ApiException;
import com.qyhl.webtv.basiclib.utils.network.model.ApiResult;
import com.qyhl.webtv.commonlib.common.CommonUtils;
import com.qyhl.webtv.commonlib.entity.civilized.PracticeAcitivityBean;
import com.qyhl.webtv.commonlib.entity.civilized.PracticeIsVolunteerBean;

public class PracticeActDetailModel implements PracticeActDetailContract.PracticeActDetailModel {
    private PracticeActDetailPresenter mPresenter;

    PracticeActDetailModel(PracticeActDetailPresenter mPresenter) {
        this.mPresenter = mPresenter;
    }

    @Override
    public void getDetail(String actId, String phone) {

        EasyHttp.get(PracticeUrl.GET_PRACTICE_ACT_DETAIL)
                .params("actId", actId)
                .params("phone", phone)
                .execute(new SimpleCallBack<PracticeAcitivityBean>() {
                    @Override
                    public void onError(ApiException e) {
                        if (e.getCode() == 202) {
                            mPresenter.getDetailError("暂无任何内容！");
                        } else {
                            mPresenter.getDetailError("获取详情出错，请重新尝试！");
                        }
                    }

                    @Override
                    public void onSuccess(PracticeAcitivityBean practiceAcitivityBean) {
                        if (practiceAcitivityBean != null) {
                            mPresenter.setDetail(practiceAcitivityBean);
                        } else {
                            mPresenter.getDetailError("暂无任何内容！");
                        }
                    }
                });

    }

    @Override
    public void doPraise(String actId, String phone) {

        EasyHttp.get(PracticeUrl.PRACTICE_DO_PRAISE)
                .params("actId", actId)
                .params("phone", phone)
                .execute(new SimpleCallBack<String>() {
                    @Override
                    public void onError(ApiException e) {
                        mPresenter.praiseError("点赞失败，请重新尝试！");
                    }

                    @Override
                    public void onSuccess(String s) {
                        ApiResult apiResult  = new Gson().fromJson(s,ApiResult.class);
                        if (apiResult.getCode() == 200) {
                            mPresenter.praiseSuccess(Integer.parseInt(apiResult.getData().toString()));
                        } else {
                            mPresenter.praiseError("点赞出错，请重新尝试！");
                        }
                    }
                });

    }

    @Override
    public void getIsVolunteer(String phone) {
        EasyHttp.post(PracticeUrl.GET_IS_VOLUNTEER)
                .params("siteId", CommonUtils.getInstance().getSite()+"")
                .params("phone", phone)
                .execute(new SimpleCallBack<PracticeIsVolunteerBean>() {
                    @Override
                    public void onError(ApiException e) {
                        mPresenter.setIsVolunteer(null);
                    }

                    @Override
                    public void onSuccess(PracticeIsVolunteerBean practiceIsVolunteerBean) {
                        mPresenter.setIsVolunteer(practiceIsVolunteerBean);
                    }
                });
    }

    @Override
    public void volunteerSign(String actId, String volId) {
        EasyHttp.post(PracticeUrl.POST_PRACTICE_VOLUNTEER_SIGN)
                .params("volId", volId)
                .params("actId", actId)
                .execute(new SimpleCallBack<String>() {
                    @Override
                    public void onError(ApiException e) {
                        mPresenter.signResult(false);
                    }

                    @Override
                    public void onSuccess(String s) {
                        ApiResult apiResult = new Gson().fromJson(s,ApiResult.class);
                        if (apiResult.getCode() == 200) {
                            mPresenter.signResult(true);
                        } else {
                            mPresenter.signResult(false);
                        }
                    }
                });
    }
}
